<template>
  <div class="app-container">
    <el-row type="flex" justify="end">
      <el-button type="success" size="medium" icon="el-icon-edit"  @click="add"
        >新建
      </el-button>
    </el-row>
   <!-- 添加用户弹出框 -->
    <el-dialog title="添加" :visible.sync="dialogFormVisible">
      <el-form :model="form">
        <el-form-item label="学习形式代码">
          <el-input v-model="form.itemCode"></el-input>
        </el-form-item>
        <el-form-item label="学习形式名称">
          <el-input v-model="form.itemName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="编辑" :visible.sync="editFormVisible">
      <el-form :model="form">
        <el-form-item label="学习形式代码">
          <el-input v-model="form.itemCode"></el-input>
        </el-form-item>
        <el-form-item label="学习形式名称">
          <el-input v-model="form.itemName"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="editSubmit">确 定</el-button>
      </div>
    </el-dialog>
    <el-table :data="list" border stripe>
      <el-table-column label="序号" align="center" type="index"></el-table-column>
      <el-table-column
        label="学习形式代码"
        prop="itemCode"
        align="center"
      ></el-table-column>
      <el-table-column
        label="学习形式名称"
        prop="itemName"
        align="center"
      ></el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button type="warning" size="mini" @click="edit(scope.row)">编辑</el-button>
          <el-button type="danger" size="mini" @click="del(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="pagination">
      <el-pagination
        :total="total"
        :current-page="pageNo"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"
        layout="total,sizes,prev,pager,next"
      >
      </el-pagination>
    </div>
  </div>
</template>

<style>
.pagination {
  margin-top: 10px;
  text-align: right;
}
</style>

<script>
import { getList, deleteData, addData, editData } from "@/api/LearningForm";
export default {
  data() {
    return {
      list: [],
      pageNo: 1,
      pageSize: 5,
      total: 0,
      pageSizes: [5, 10, 15, 20, 25],
      methodType: "", //操作类型：添加、更新
      dialogFormVisible: false,
      editFormVisible: false,
      form: {},
      currentObj: {},
    };
  },
  watch: {
    pageNo: function () {
      this.fetchData();
    },
    pageSize: function () {
      this.fetchData();
    },
    checkHobby: function () {
      this.editForm.hobby = this.checkHobby.toString();
    },
  },
  mounted() {
    this.fetchData();
  },
  methods: {
    indexMethod(index) {
      return index * 2;
    },
    submit() {
      this.dialogFormVisible = false;
      addData(this.form)
        .then((res) => {
          if (res === 1) {
            this.$message.success("添加成功");
          }
        })
        .catch((err) => {
          this.$message.error("添加失败");
        });
      this.form = {};
    },
    add() {
      this.dialogFormVisible = true;
    },
    edit(data) {
      this.editFormVisible = true;
      this.currentObj = data;
    },
    editSubmit() {
      this.editFormVisible = false;
      let data = {};
      data.itemCode = this.form.itemCode;
      data.itemName = this.form.itemName;
      data.id = this.currentObj.id;

      editData(data)
        .then((res) => {
          this.$message.success("添加成功");
          this.currentObj.itemCode = data.itemCode;
          this.currentObj.itemName = data.itemName;
        })
        .catch((err) => {
          this.$message.error("更新失败");
        });
      this.form = {};
    },
    fetchData() {
      var data = {
        //分页数据
        pageSize: this.pageSize,
        pageNo: this.currentPage,
      };
      getList(data).then((res) => {
        this.list = res.resultList;
        this.total = res.total;
        this.listLoading = false;
      });
    },
    handleCurrentChange(val) {
      this.pageNo = val;
    },
    handleSizeChange(val) {
      this.pageSize = val;
      this.pageNo = 1;
    },
    search() {
      this.fetchData();
    },
    reset() {
      this.$refs["sf"].resetFields();
      this.pageNo = 1;
      this.fetchData();
    },
    del(data) {
      console.log(data);
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteData(data)
            .then((res) => {
              this.$message.success("删除成功");
            })
            .catch((err) => {
              this.$message.error("删除失败");
            });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    currentChange(current) {
      let data = {
        pageSize: 10,
        pageNo: current,
      };
      getList(data).then((res) => {
        this.tableData = res.resultList;
      });
    },
  },
  computed: {},
};
</script>
<style scoped>
.el-button--text {
  color: #409eff;
}

.add {
  float: right;
  margin-bottom: 10px;
}

.el-form-item__content {
  background-color: #333 !important;
}
</style>
